package com.bidevalution.business.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.bidevalution.business.service.BeMethodStepService;
import com.bidevalution.dao.entity.BeLayoutConfig;
import com.bidevalution.dao.entity.BeMethodHeader;
import com.bidevalution.dao.entity.BeMethodLayout;
import com.bidevalution.dao.entity.BeMethodStep;
import com.bidevalution.dao.mapper.BeLayoutConfigMapper;
import com.bidevalution.dao.mapper.BeMethodLayoutMapper;
import com.bidevalution.dao.mapper.BeMethodStepMapper;
import com.bidevalution.support.dto.TreeNodeDTO;

@Service
public class BeMethodStepServiceImpl implements BeMethodStepService {
	@Autowired
	private BeMethodStepMapper beMethodStepMapper;
	@Override
	public List<BeMethodStep> selectForListByParentId(String defineId, String parentId) {

		return beMethodStepMapper.selectForListByParentId(defineId, parentId);
	}

	@Override
	@Transactional
	public void insert(BeMethodStep beMethodStep) {
		beMethodStepMapper.insert(beMethodStep);
	}

	@Override
	public void update(BeMethodStep beMethodStep) {
		beMethodStepMapper.updateByPrimaryKey(beMethodStep);
	}

	@Override
	public BeMethodStep selectById(String seqId) {
		return beMethodStepMapper.selectByPrimaryKey(seqId);
	}

	@Override
	public void deleteById(String seqId) {
		beMethodStepMapper.deleteByPrimaryKey(seqId);
	}

	@Override
	public List<TreeNodeDTO> selectTree(String defineId, String parentId) {
		List<TreeNodeDTO> treeNodeDTOs = new ArrayList<TreeNodeDTO>();
		TreeNodeDTO treeNodeDTO;
		for (BeMethodStep beMethodStep : beMethodStepMapper.selectForListByParentId(defineId, parentId)) {
			if(!"1".equals(beMethodStep.getStepCode()) && !"2".equals(beMethodStep.getStepCode())) {
				treeNodeDTO = new TreeNodeDTO();
				treeNodeDTO.setId(beMethodStep.getSeqId());
				treeNodeDTO.setLabel(beMethodStep.getStepName());
				treeNodeDTO.setParentId(parentId);
				treeNodeDTO.setChildren(getChildrenNode(defineId, beMethodStep.getSeqId()));
				treeNodeDTOs.add(treeNodeDTO);
			}
			
		}
		return treeNodeDTOs;
	}

	/**
	 * 递归算法解析成树形结构
	 *
	 * @param defineId
	 * @param seqId
	 * @return
	 * @author daizhifeng
	 */
	public List<TreeNodeDTO> getChildrenNode(String defineId, String seqId) {
		List<TreeNodeDTO> childTreeNodes = new ArrayList<TreeNodeDTO>();
		// 遍历子节点
		for (BeMethodStep beMethodStep : beMethodStepMapper.selectForListByParentId(defineId, seqId)) {
			TreeNodeDTO treeNodeDTO = new TreeNodeDTO();
			treeNodeDTO.setId(beMethodStep.getSeqId());
			treeNodeDTO.setLabel(beMethodStep.getStepName());
			treeNodeDTO.setParentId(seqId);
			treeNodeDTO.setChildren(getChildrenNode(defineId, beMethodStep.getSeqId()));
			childTreeNodes.add(treeNodeDTO);
		}
		return childTreeNodes;
	}

	@Override
	public List<BeMethodStep> selectListByMethodId(String methodId) {

		return beMethodStepMapper.selectListByMethodId(methodId);
	}

}
